package com.example.haiti.infrastructure.surveyor;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class questionnaire_Activity3 extends Activity {
	
    private Utils utilsHelper = null;
    private Cursor structureCursor = null;
    private int structureID;
	
	private EditText Edit_nFloors  = null;    
    private EditText Edit_nChildren= null;
    private EditText Edit_nAdults= null;
    private Spinner spinner_SteelWalls = null;
    private Spinner spinner_RoofType = null;  
    
    
    private static final int STEEL_WALLS_ADAPTOR_ID = 0;
    private static final int ROOF_TYPE_ADAPTOR_ID = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.questionnaire_layout_3);
                
        utilsHelper = new Utils(questionnaire_Activity3.this);      

        utilsHelper.openDatabase(); 

    	getStructure();       
   	       
        // Initializes view
        manageEditTextFields();
        initializeSpinners();
        setupNextButton();
                 
    } // end of the onCreate method
    
    @Override
    protected void onPause() {
        super.onPause();

        utilsHelper.closeDatabase();
    }
    
    @Override
    protected void onStop() {
      super.onStop();
       
    	utilsHelper.closeDatabase();
    }
    
    private void getStructure(){
    	// Get structure ID
        structureID = getIntent().getIntExtra("structureID", 1000); //Get structureID otherwise 1000      
        structureCursor = utilsHelper.getStructureCursor(structureID);	    	
    }
    
    private void manageEditTextFields(){
    	
    	// Declares the text input fields
	    Edit_nFloors = (EditText) this.findViewById(R.id.Edit_nFloors);     
	    Edit_nChildren = (EditText) this.findViewById(R.id.Edit_nChildren); 
	    Edit_nAdults = (EditText) this.findViewById(R.id.Edit_nAdults); 	  
	    
        // Checks if data already exists
        String nFloors= structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_FLOORS));
		String nChildren = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_CHILDREN));
		String nAdults = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_ADULTS));
		
		// Sets the text of the input fields
	    Edit_nFloors.setText(nFloors);
	    Edit_nChildren.setText(nChildren);
	    Edit_nAdults.setText(nAdults);

    }   
    
 private void initializeSpinners(){
    	
    	// Ownership Array Adapter
        ArrayAdapter<CharSequence> adapter_SteelWalls = ArrayAdapter.createFromResource(
        		this, R.array.SteelWallsAnswers, android.R.layout.simple_spinner_item );
      			adapter_SteelWalls.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

      			spinner_SteelWalls = (Spinner) findViewById( R.id.spinner_SteelWalls);
      			spinner_SteelWalls.setAdapter( adapter_SteelWalls );
      			
      		//  Sets the location of spinner to saved value if there is one\
      			int steelWallPosition = getArrayPosition(STEEL_WALLS_ADAPTOR_ID);
      			spinner_SteelWalls.setSelection(steelWallPosition, true);
      			      			
       // Construction Years Array Adapter
       ArrayAdapter<CharSequence> adapter_RoofType = ArrayAdapter.createFromResource(
    		   	this, R.array.RoofTypeAnswers, android.R.layout.simple_spinner_item );
       			adapter_RoofType.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);      		  
       	        
       			spinner_RoofType = (Spinner) findViewById( R.id.spinner_RoofType);
       	        spinner_RoofType.setAdapter( adapter_RoofType );
       	        
      		//  Sets the location of spinner to saved value if there is one
      			int roofType = getArrayPosition(ROOF_TYPE_ADAPTOR_ID);
       	        spinner_RoofType.setSelection(roofType,true);    	
    }
 
 	private int getArrayPosition(int spinnerType){
 		
 		int position=0;
 		
 		switch (spinnerType){
 		
 			case STEEL_WALLS_ADAPTOR_ID:
 				
 				ArrayAdapter steelWall_Adapter = (ArrayAdapter) spinner_SteelWalls.getAdapter();			
 	 			String steelWallsValue= structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_STEEL_WALLS));
 	 			position = steelWall_Adapter.getPosition(steelWallsValue);
 				
 				break;
 				
 			case ROOF_TYPE_ADAPTOR_ID:
 				
 	 			ArrayAdapter roofType_Adapter = (ArrayAdapter) spinner_RoofType.getAdapter();
 	 			String roofTypeValue = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_ROOF_TYPE));
 	 			position = roofType_Adapter.getPosition(roofTypeValue);
 				
 				break; 	
 				
 			default:				
 				break;
 			 		 		 		
 		}							
 		
 		return position;
 	}
   
    private void setupNextButton(){
    	 	
    	// Add Next button
        Button btnNextQuestions = (Button) findViewById(R.id.buttonNext);
        btnNextQuestions.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                   	
             // Defines strings input in by the user from Edit Text Fields
                String nFloors = Edit_nFloors.getText().toString();
                String nChildren = Edit_nChildren.getText().toString();
                String nAdults = Edit_nAdults.getText().toString();
                
             // Defines values input from Spinners
                String steelWalls = spinner_SteelWalls.getSelectedItem().toString();
                String roofType = spinner_RoofType.getSelectedItem().toString();
                                                             
                String[][] databaseUpdates = {
                        {SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_FLOORS, nFloors},
                        {SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_CHILDREN, nChildren},
                        {SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_ADULTS, nAdults},
                        {SqlHelper.COLUMN_STRUCTURE_STEEL_WALLS, steelWalls},
                        {SqlHelper.COLUMN_STRUCTURE_ROOF_TYPE, roofType}
                        };
                
                utilsHelper.dbHelper.updateStructureEntry(databaseUpdates, structureID );              
               
                utilsHelper.startNextActivity(structureID, exterior_Activity.class);
                
            }
        }); // End of  Next button click listener            
    }       
    
    // Back Button Pressed
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {
            utilsHelper.startNextActivity(structureID, questionnaire_Activity2.class);
        }
        return super.onKeyDown(keyCode, event);
    }
} 